<?php
class ModelSettingStore extends Model {
    public function addStore($data) {

        $insert = $this->pdo->prepare("INSERT INTO " . $this->db_prefix . "store 
            SET name = :config_name, 
            http_server  = :http_server, 
            https_server = :https_server");

        $insert->execute(array(
            ':config_name'  => $data['config_name'], 
            ':http_server'  => $data['http_server'], 
            ':https_server' => $data['https_server'], 
        ));
 
        $this->cache->delete('store');

        return $this->pdo->lastInsertId();
    }

    public function editStore($store_id, $data) {
        $update = $this->pdo->prepare("UPDATE " . $this->db_prefix . "store 
            SET name = :config_name, 
            http_server  = :http_server, 
            https_server = :https_server  
            WHERE store_id = :store_id");

        $update->execute(array(
            ':config_name'  => $data['config_name'], 
            ':http_server'  => $data['http_server'], 
            ':https_server' => $data['https_server'], 
            ':store_id'     => (int)$store_id
        ));

        $this->cache->delete('store');
    }

    public function deleteStore($store_id) {
        $this->db->query("DELETE FROM " . $this->db_prefix . "store WHERE store_id = '" . (int)$store_id . "'");

        $this->cache->delete('store');
    }	

    public function getStore($store_id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . $this->db_prefix . "store WHERE store_id = '" . (int)$store_id . "'");

        return $query->row;
    }

    public function getStores($data = array()) {
        $store_data = $this->cache->get('store');

        if (!$store_data) {
            $query = $this->db->query("SELECT * FROM " . $this->db_prefix . "store ORDER BY http_server");

			$store_data = $query->rows;
		
			$this->cache->set('store', $store_data);
		}
	 
		return $store_data;
	}

	public function getTotalStores() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "store");
		
		return $query->row['total'];
	}	
	
	public function getTotalStoresByLayoutId($layout_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_layout_id' AND `value` = '" . (int)$layout_id . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}
	
	public function getTotalStoresByLanguage($language) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_language' AND `value` = '" . $this->db->escape($language) . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}
	
	public function getTotalStoresByCurrency($currency) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_currency' AND `value` = '" . $this->db->escape($currency) . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}
	
	public function getTotalStoresByCountryId($country_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_country_id' AND `value` = '" . (int)$country_id . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}
	
	public function getTotalStoresByZoneId($zone_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_zone_id' AND `value` = '" . (int)$zone_id . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}
	
	public function getTotalStoresByCustomerGroupId($user_group_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_user_group_id' AND `value` = '" . (int)$user_group_id . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}	
	
	public function getTotalStoresByInformationId($information_id) {
      	$account_query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_account_id' AND `value` = '" . (int)$information_id . "' AND store_id != '0'");
      	
		$checkout_query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_checkout_id' AND `value` = '" . (int)$information_id . "' AND store_id != '0'");
		
		return ($account_query->row['total'] + $checkout_query->row['total']);
	}
	
	public function getTotalStoresByOrderStatusId($order_status_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db_prefix . "setting WHERE `key` = 'config_order_status_id' AND `value` = '" . (int)$order_status_id . "' AND store_id != '0'");
		
		return $query->row['total'];		
	}	
}
?>
